package cn.paraview.mfa.http;

import java.io.File;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import android.os.AsyncTask;
import cn.paraview.mfa.Utils;
import cn.paraview.mfa.bean.JasonBean;
import cn.paraview.mfa.callback.RequestCallback;
import cn.paraview.mfa.tools.PreferUtil;
import cn.paraview.mfa.tools.PreferUtil.PreferenceKey;

public class HttpPostTask extends AsyncTask<Void, Void, String> {

	private String url;
	private Map<String, Object> params;
	private Map<String, File> files;
	private Map<String, String> headerParams;
	private RequestCallback requestCallback;
	private int requestId;

	public HttpPostTask(String url,Map<String, Object> params,
			Map<String, File> files,Map<String, String> headerParams,
			RequestCallback requestCallback,int requestId) {
		this.url = url;
		this.params = params;
		this.files = files;
		this.headerParams = headerParams;
		this.requestCallback = requestCallback;
		this.requestId = requestId;
	}

	@Override
	protected String doInBackground(Void... params) {
		String response = "";
		response = HttpRequest.http_post(url, this.params, files, headerParams);
		return response;
	}

	@Override
	protected void onPostExecute(String result) {
		JasonBean jasonBean = null;
		try{
			jasonBean = JSON.parseObject(result, JasonBean.class);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (jasonBean != null && requestId == HttpId.GET_REFRESH_TOKEN_ID) {
			Utils.refresh_token = jasonBean.getRefresh_token();
			Utils.token = jasonBean.getAccess_token();
			PreferUtil.savePrivateUserData(Utils.context,
					PreferenceKey.PRIVATE_TOKEN, Utils.token);
			PreferUtil.savePrivateUserData(Utils.context,
					PreferenceKey.REFRESH_TOKEN, Utils.refresh_token);
		}
		requestCallback.onResponseBack(requestId, jasonBean);
	}

}
